package com.dex4d.ui.wallet.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;

import com.dex4d.R;
import com.dex4d.async.KeyAsyncTask;
import com.dex4d.core.AppToolBarBaseActivity;
import com.dex4d.dialog.CommonMessageDialog;
import com.dex4d.utils.BaseUtils;
import com.dex4d.utils.EthWalletUtil;
import com.dex4d.utils.LogUtils;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by chengyuchun on 2018/10/29.
 */
public class RestoreMnemonicActivity extends AppToolBarBaseActivity {

    @BindView(R.id.et_mnemonic)
    EditText mEtMnemonic;

    @BindView(R.id.btn_confirm)
    Button mBtnConfirm;

    @Override
    protected void initToolbar() {
        showBackView();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_restore_mnemonic;
    }

    @Override
    protected void initVariables() {
        mEtMnemonic.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.length() >0){
                    mBtnConfirm.setBackgroundResource(R.drawable.btn_blue_clickable_bg);
                    mBtnConfirm.setTextColor(getResources().getColor(R.color.white));
                    mBtnConfirm.setClickable(true);
                }else {
                    mBtnConfirm.setBackgroundResource(R.drawable.btn_blue_unclick_bg);
                    mBtnConfirm.setTextColor(getResources().getColor(R.color.gray_B7C5CA));
                    mBtnConfirm.setClickable(false);
                }
            }
        });
        mBtnConfirm.setClickable(false);
    }

    @Override
    protected void loadData() {

    }

    private String formatString(String str){
        if(TextUtils.isEmpty(str)){
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for(char c: str.toCharArray()){
            if((c>=97 && c<=122) || (c>=65 && c<=90)){
                sb.append(c);
            }else {
                sb.append(" ");
            }
        }
        return sb.toString();
    }

    @OnClick(R.id.btn_confirm)
    public void onViewClick(){
        String[] arrays = formatString(mEtMnemonic.getText().toString()).split("\\s+");

        final List<String> mnemonicList = Arrays.asList(arrays);
        LogUtils.d(mnemonicList);

        if(mnemonicList.size()!=12){
            CommonMessageDialog dialog = new CommonMessageDialog(this,CommonMessageDialog.DialogStyle.TIP);
            dialog.setMsg(getString(R.string.phrase_error_prompt));
            dialog.setLeftBtnTxt(getString(R.string.ok));
            dialog.show();
        }else {
            showLoadingDialog();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    final boolean isOk = EthWalletUtil.checkMnemonic(mnemonicList); //验证助记词
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (isOk) {
                                String randomPwd = BaseUtils.getRandomPwd(); //6位随机密码
                                new KeyAsyncTask(randomPwd,mnemonicList,true,RestoreMnemonicActivity.this).execute(); //创建钱包
                            } else {
                                dismissLoadingDialog();
                                CommonMessageDialog dialog = new CommonMessageDialog(RestoreMnemonicActivity.this,CommonMessageDialog.DialogStyle.TIP);
                                dialog.setMsg(getString(R.string.phrase_error_prompt));
                                dialog.setLeftBtnTxt(getString(R.string.ok));
                                dialog.show();
                            }
                        }
                    });
                }
            }).start();
        }
    }

    @Override
    protected void onDestroy() {
        dismissLoadingDialog();
        super.onDestroy();
    }

}
